package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.domain.Visitor;
import cn.wolfcode.wolf2w.redis.service.IVisitorRedisService;
import cn.wolfcode.wolf2w.repository.VisitorRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.service.IVisitorService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class VisitorServiceImpl implements IVisitorService {
    @Autowired
    private VisitorRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IVisitorRedisService visitorRedisService;
    @Override
    public void save(Visitor visitor) {
        visitor.setId(null);
        repository.save(visitor);
    }

    @Override
    public void update(Visitor visitor) {
        repository.save(visitor);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Visitor get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Visitor> list() {
        return repository.findAll();
    }

    @Override
    public Visitor visitor(String ownerId, String currentUId) {
        AssertUtil.hasLength(currentUId, "请先登录再操作");
        AssertUtil.hasLength(ownerId, "异常的操作");
        // 先查询数据是否为空，为空：创建数据
        Visitor visitor = repository.findByUid(ownerId);
        if (visitor == null) {
            Visitor vs = new Visitor();
            vs.setUid(ownerId);
            vs.setVisitorId(new ArrayList<>());
            this.save(vs);
        }
        // 设置好对象后再获取
        Visitor vs = visitor == null ? repository.findByUid(ownerId) : visitor;
        //当前用户访问自己的窝
        if (currentUId.equals(ownerId)){
            //进入自己的窝，不需要执行其他的操作，直接查询数据返回即可
            // 根据visitorId查询visitor
            List<UserInfo> list = new ArrayList<>();
            for (String id : vs.getVisitorId()) {
                UserInfo userInfo = userInfoService.get(id);
                list.add(userInfo);
            }
            vs.setVisitors(list);
            // 获取今日访客
            int todayView = visitorRedisService.getTodayView(ownerId);
            vs.setTodayView(todayView);
        }else {
            //当前用户访问别人的窝
            vs.setTotalView(vs.getTotalView() + 1); // 总访问量 +1
            visitorRedisService.incrementValue(ownerId); // 今日访问量 +1
            vs.setTodayView(visitorRedisService.getTodayView(ownerId));
            List<String> visitorId = vs.getVisitorId();
            // 判断该用户的窝是否包含我的id
            if (visitorId.contains(currentUId)) {
                // 包含移除再添加
                visitorId.remove(currentUId);
                visitorId.add(0,currentUId);
            }else {
                // 不包含直接添加
                visitorId.add(0,currentUId);
            }
            List<UserInfo> list = new ArrayList<>();
            for (String id : visitorId) {
                UserInfo userInfo = userInfoService.get(id);
                list.add(userInfo);
            }
            vs.setVisitors(list);
            this.update(vs);
        }
        return vs;
    }
}
